package com.youlu.campus.admin.community.controller;

import com.youlu.campus.admin.utils.AuthUtils;
import com.youlu.campus.common.base.RestResult;
import com.youlu.campus.common.base.RestResultBuilder;
import com.youlu.campus.entity.ActivityPointConfig;
import com.youlu.campus.entity.VO.req.ActivityPointConfigQueryVO;
import com.youlu.campus.service.activity.ActivityPointConfigService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.repository.support.PageableExecutionUtils;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.Objects;

@Slf4j
@RestController
public class ActivityPointConfigController {
    @Autowired
    private ActivityPointConfigService activityPointConfigService;

    @PostMapping("/activity/point-config/list")
    public RestResult list(@RequestBody @Valid ActivityPointConfigQueryVO req) {
        String comId = AuthUtils.getComId();
        Page<ActivityPointConfig> users = activityPointConfigService.list(req);
        if (users == null) {
            PageRequest pageRequest = PageRequest.of(req.getPage() - 1, req.getPageSize());
            users = PageableExecutionUtils.getPage(new ArrayList<>(), pageRequest, () -> 0L);
        }
        return RestResultBuilder.builder().success().data(users).build();
    }


    @PostMapping("/activity/point-config/create")
    public RestResult create(@RequestBody @Valid ActivityPointConfig req) {
        String comId = AuthUtils.getComId();
        req.setTenantId(comId);
        boolean re = activityPointConfigService.create(req);
        if (re) {
            return RestResultBuilder.builder().success().build();
        }
        return RestResultBuilder.builder().failure().build();
    }

    @PostMapping("/activity/point-config/update")
    public RestResult update(@RequestBody @Valid ActivityPointConfig req) {
        String comId = AuthUtils.getComId();
        req.setTenantId(comId);
        boolean re = activityPointConfigService.update(req);
        if (re) {
            return RestResultBuilder.builder().success().build();
        }
        return RestResultBuilder.builder().failure().build();
    }

    @GetMapping("/activity/point-config/delete")
    public RestResult delete(@RequestParam("id") String id) {
        String comId = AuthUtils.getComId();
        boolean re = activityPointConfigService.delete(id);
        if (re) {
            return RestResultBuilder.builder().success().build();
        }
        return RestResultBuilder.builder().failure().build();
    }

    @GetMapping("/activity/point-config/find-one")
    public RestResult findOne(@RequestParam("id") String id) {
        String comId = AuthUtils.getComId();
        ActivityPointConfig r = activityPointConfigService.findOne(id);
        if (Objects.nonNull(r)) {
            return RestResultBuilder.builder().success(r).build();
        }
        return RestResultBuilder.builder().failure().build();
    }
}
